package shuishijie.suishijie.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.widget.Toast;

/**
 * Created by mindx on 2017/8/17.
 */

public abstract class BaseFragment extends Fragment {

    /**
     * activity回调,向activity发送消息
     */
    protected CallBacks mCallBack = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void startActivity(Class<? extends Activity> clazz) {
        super.startActivity(new Intent(getContext(), clazz));
    }

    /**
     * 监听屏幕方向更改
     */
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    /**
     * fragment添加到activity中触发
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallBack = (CallBacks) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement BaseFragment.CallBacks");
        }

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallBack = null;
    }

    public void updateFragment(int type, Object params) {

    }

    /**
     * 回调activity
     */
    public interface CallBacks {
        Object onFragmentCallback(int type, Object params);
    }

    Toast toast = null;

    public void showMessage(String msg) {
        if (getContext() == null) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    public void hideMessage() {
        if (toast != null) {
            toast.cancel();
        }
    }

    public static String getMoneyStr(double money) {
        return getMoneyStr(money, true);
    }

    public static String getMoneyStr(double money, boolean mark) {
        return (mark ? "¥" : "") + String.format("%.2f", money);
    }

}